//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	Dictionary.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.Serialization;

namespace Smart
{
	/// <summary>
	/// Dictionary of objects of <typeparamref name="TObjectType"/> to dictionary of keys of <typeparamref name="TKeyType"/> to item of <typeparamref name="TItemType"/>
	/// </summary>
	[Serializable]
	[DebuggerStepThrough]
	public class Dictionary<TObjectType, TKeyType, TItemType> : Dictionary<TObjectType, Dictionary<TKeyType, TItemType>>
	{
		public virtual bool SafeExists(TObjectType objectId, TKeyType itemId)
		{
			var res = false;

			Dictionary<TKeyType, TItemType> tmp = null;

			TryGetValue(objectId, out tmp);

			if (tmp != null)
				res = tmp.ContainsKey(itemId);

			return res;
		}

		public virtual TItemType SafeGet(TObjectType objectId, TKeyType itemId)
		{
			return SafeGet(objectId, itemId, default(TItemType));
		}

		public virtual TItemType SafeGet(TObjectType objectId, TKeyType itemId, TItemType defaultValue)
		{
			var res = defaultValue;

			Dictionary<TKeyType, TItemType> tmp;

			TryGetValue(objectId, out tmp);

			if (tmp != null && !tmp.TryGetValue(itemId, out res))
				res = defaultValue;

			return res;
		}

		public virtual Dictionary<TKeyType, TItemType> SafeGet(TObjectType objectId)
		{
			Dictionary<TKeyType, TItemType> tmp;

			TryGetValue(objectId, out tmp);

			return tmp;
		}

		public virtual void SafeSet(TObjectType objectId, TKeyType itemId, TItemType item)
		{
			Dictionary<TKeyType, TItemType> tmp = null;

			TryGetValue(objectId, out tmp);

			if (tmp == null)
				this[objectId] = tmp = SafeCreateChildDictionary();

			if (tmp != null)
				tmp[itemId] = item;
		}

		public virtual Dictionary<TKeyType, TItemType> SafeCreateChildDictionary()
		{
			return new Dictionary<TKeyType, TItemType>();
		}

		public virtual void SafeSet(TObjectType objectId, Dictionary<TKeyType, TItemType> items)
		{
			this[objectId] = items;
		}

		public virtual void SafeRemove(TObjectType objectId, TKeyType itemId)
		{
			Dictionary<TKeyType, TItemType> tmp = null;

			TryGetValue(objectId, out tmp);

			if (tmp == null)
				return;

			tmp.Remove(itemId);

			if (tmp.Count <= 0)
				Remove(objectId);
		}

		/// <summary>
		/// Dictionary of objects of <typeparamref name="TObjectType"/> to dictionary of keys of <typeparamref name="TKeyType"/> to item of <typeparamref name="TItemType"/>
		/// </summary>
		public Dictionary()
		{
		}

		/// <summary>
		/// Dictionary of objects of <typeparamref name="TObjectType"/> to dictionary of keys of <typeparamref name="TKeyType"/> to item of <typeparamref name="TItemType"/>
		/// </summary>
		public Dictionary(IEqualityComparer<TObjectType> comparer)
			: base(comparer)
		{
		}

		/// <summary>
		/// Dictionary of objects of <typeparamref name="TObjectType"/> to dictionary of keys of <typeparamref name="TKeyType"/> to item of <typeparamref name="TItemType"/>
		/// </summary>
		public Dictionary(int capacity)
			: base(capacity)
		{
		}

		/// <summary>
		/// Dictionary of objects of <typeparamref name="TObjectType"/> to dictionary of keys of <typeparamref name="TKeyType"/> to item of <typeparamref name="TItemType"/>
		/// </summary>
		public Dictionary(int capacity, IEqualityComparer<TObjectType> comparer)
			: base(capacity, comparer)
		{
		}

		/// <summary>
		/// Dictionary of objects of <typeparamref name="TObjectType"/> to dictionary of keys of <typeparamref name="TKeyType"/> to item of <typeparamref name="TItemType"/>
		/// </summary>
		protected Dictionary(SerializationInfo info, StreamingContext context)
			: base(info, context)
		{
		}
	}
}